<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./iconfont/iconfont.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .location {
            position: relative;
            display: flex;
            gap: 10px;
            height: 100px;
            padding: 10px;
        }

        .location .province,
        .location .city,
        .location .county {
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 150px;
            height: 40px;
            border-radius: 5px;
            padding: 0 5px;
            font-size: 14px;
            border: 1px solid #ccc;
            color: #ccc;

            cursor: not-allowed;
            pointer-events: none;
        }

        .haircut {
            width: 20px;
            height: 20px;
            background-size: 100% 100%;
            font-weight: bold;
            text-align: center;
            line-height: 20px;
            transition: all 0.5s;
        }

        .items {
            position: absolute;
            padding: 15px;
            font-size: 12px;
            border: 1px solid #ccc;
            box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
            color: #666;
            transform: rotateX(-90deg);
            transform-origin: top center;
            opacity: 0;
            transition: all 0.5s;
        }

        .itemProvince {
            top: 60px;
            left: 10px;
            width: 360px;
        }

        .itemCity {
            top: 60px;
            left: 170px;
            width: 178px;
        }

        .itemCounty {
            top: 60px;
            left: 330px;
            width: 178px;
        }

        .items ul li {
            list-style: none;
            width: 58px;
            padding: 5px;
            border-radius: 5px;
            cursor: pointer;
        }

        .items ul {
            display: flex;
            flex-wrap: wrap;
            gap: 0px 30px;
        }

        .items div {
            position: absolute;
            top: -5px;
            left: 75px;
            width: 10px;
            height: 10px;
            background-color: white;
            border-top: 1px solid #ccc;
            border-left: 1px solid #ccc;
            transform: rotate(45deg);
        }

        .rotate {
            transform: rotate(180deg);
        }

        .appear {
            opacity: 1;
            transform: rotateX(0deg);
        }
    </style>
</head>

<body>
    <div class="location">
        <div class="province">
            <div>请选择省份</div>
            <div class="haircut">
                <span class="iconfont icon-sanjiaojiantou-xia"></span>
            </div>
        </div>
        <div class="city">
            <div>请选择城市</div>
            <div class="haircut">
                <span class="iconfont icon-sanjiaojiantou-xia"></span>
            </div>
        </div>
        <div class="county">
            <div>请选择地区</div>
            <div class="haircut">
                <span class="iconfont icon-sanjiaojiantou-xia"></span>
            </div>
        </div>

        <div class="itemProvince items">
            <div></div>
            <ul></ul>
        </div>

        <div class="itemCity items">
            <div></div>
            <ul></ul>
        </div>

        <div class="itemCounty items">
            <div></div>
            <ul></ul>
        </div>
    </div>
    <script>
        const items = document.querySelector(".items");

        // 定义一个函数来获取数据
        function getData(url) {
            return new Promise(function (resolve, reject) {
                const xhr = new XMLHttpRequest();
                xhr.open("GET", url);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        resolve(JSON.parse(xhr.responseText));
                    }
                };
                xhr.send();
            });
        }

        // 读取数据并保存到变量中
        const Arr = [];
        async function sendData() {
            const datas = await getData("http://project.x-zd.net:3001/apis/citylist");
            const res = datas.data;
            res.forEach(item => {
                Arr.push(item);
            });

            console.log(Arr);

            provinceList.querySelector("ul").innerHTML = Arr.map(item => {
                return `<li>${item.label}</li>`;
            }).join("");
        }

        sendData();

        // 获取省市县显示框元素
        const province = document.querySelector(".province");
        const city = document.querySelector(".city");
        const county = document.querySelector(".county");

        // 获取省市县显示框元素的下拉列表元素
        const provinceList = document.querySelector(".itemProvince");
        const cityList = document.querySelector(".itemCity");
        const countyList = document.querySelector(".itemCounty");
        const itemss = document.querySelectorAll(".items");

        // 互斥方法
        function hideAll() {
            itemss.forEach(item => {
                item.classList.remove("appear");
            });
            document.querySelectorAll(".location .haircut").forEach(item => {
                item.classList.remove("rotate");
            });
        }

        // 城市数组
        var cityArr = [];
        // 地区数组
        var countyArr = [];

        // 监听页面点击事件
        province.style.cursor = "pointer";
        province.style.pointerEvents = "auto";
        province.style.color = "#666";

        window.onload = function () {
            document.addEventListener("click", function (e) {
                city.style = ''
                county.style = ''
                // 清除通过“style：
                if (province.querySelectorAll("div")[0].innerText !== "请选择省份") {
                    city.style.cursor = "pointer";
                    city.style.pointerEvents = "auto";
                    city.style.color = "#666";
                }
                if (city.querySelectorAll("div")[0].innerText !== "请选择城市") {
                    county.style.cursor = "pointer";
                    county.style.pointerEvents = "auto";
                    county.style.color = "#666";
                }
            });
        };

        //通过父元素
        // 省级显示框点击事件
        province.addEventListener("click", function () {
            hideAll();

            // 显示省级下拉列表
            provinceList.classList.add("appear");
            province.querySelector('.haircut').classList.add("rotate");

            provinceList.querySelectorAll("li").forEach(item => {
                item.style.cursor = "pointer";
                item.addEventListener("click", function (a) {
                    provinceList.querySelectorAll("li").forEach(item => {
                        item.style.backgroundColor = "";
                    });

                    // 恢复下一级显示框默认值
                    county.querySelectorAll("div")[0].innerText = "请选择地区";
                    city.querySelectorAll("div")[0].innerText = "请选择城市";

                    province.querySelectorAll("div")[0].innerText = item.innerText;
                    item.style.backgroundColor = "#ccc";

                    provinceList.classList.remove("appear");
                    province.querySelector('.haircut').classList.remove("rotate");

                    // 点击后给城市列表赋值
                    console.log(Arr);
                    cityArr = Arr.filter(e => {
                        return e.label === item.innerText;
                    });

                    cityList.querySelector("ul").innerHTML = cityArr[0].children.map(e => {
                        return `<li>${e.label}</li>`;
                    }).join("");
                });
            });
        });

        // 市级显示框点击事件
        city.addEventListener("click", function () {
            hideAll();

            // 显示市级下拉列表
            cityList.classList.add("appear");
            city.querySelector('.haircut').classList.add("rotate");

            cityList.querySelectorAll("li").forEach(item => {
                item.style.cursor = "pointer";
                item.addEventListener("click", function () {
                    cityList.querySelectorAll("li").forEach(item => {
                        item.style.backgroundColor = "";
                    });

                    // 恢复下一级显示框默认值
                    county.querySelectorAll("div")[0].innerText = "请选择地区";

                    city.querySelectorAll("div")[0].innerText = item.innerText;
                    item.style.backgroundColor = "#ccc";

                    cityList.classList.remove("appear");
                    city.querySelector('.haircut').classList.remove("rotate");

                    // 点击后给地区列表赋值
                    countyArr = cityArr[0].children.filter(e => {
                        return e.label === item.innerText;
                    });

                    countyList.querySelector("ul").innerHTML = countyArr[0].children.map(e => {
                        return `<li>${e.label}</li>`;
                    }).join("");
                });
            });
        });

        // 县级显示框点击事件
        county.addEventListener("click", function () {
            hideAll();

            // 显示县级下拉列表
            countyList.classList.add("appear");
            county.querySelector('.haircut').classList.add("rotate");

            countyList.querySelectorAll("li").forEach(item => {
                item.style.cursor = "pointer";
                item.addEventListener("click", function () {
                    countyList.querySelectorAll("li").forEach(item => {
                        item.style.backgroundColor = "";
                    });

                    county.querySelectorAll("div")[0].innerText = item.innerText;
                    item.style.backgroundColor = "#ccc";

                    countyList.classList.remove("appear");
                    county.querySelector('.haircut').classList.remove("rotate");
                });
            });
        });
    </script>
</body>

</html>